package com.boot.chap01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 阻塞I/O示例
 *
 * @author renwq
 * @since 2021/9/19 1:55
 */
public class BlockingIoExample {

    public void serve(int port) throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        Socket socket = serverSocket.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        String request, response;
        while ((request = in.readLine()) != null) {
            if (request.equals("Done")) {
                break;
            }
            response = processResponse(request);
            out.println(response);
        }
    }

    public String processResponse(String request) {
        return "Processed";
    }
}
